package cn.homework.dao.impl;

import cn.homework.dao.ProductDao;
import cn.homework.dao.ShoppingCarDao;
import cn.homework.domain.OrderDetails;
import cn.homework.domain.Product;
import cn.homework.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author ACT
 */
public class ShoppingCarDaoImpl implements ShoppingCarDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<OrderDetails> findAllShoppingCart() {
        String sql = "select * from orderdetails";
        List<OrderDetails> orderDetailsList = template.query(sql, new BeanPropertyRowMapper<>(OrderDetails.class));
        return orderDetailsList;
    }

    @Override
    public void deldelChooseShoppingCartProduct(String orderId) {
        String sql = "delete from orderdetails where order_id = ?";
        template.update(sql, orderId);
    }

    @Override
    public void emptyShoppingCart() {
        String sql = "delete from orderdetails";
        template.update(sql);
    }

    @Override
    public void addProductToShoppingCart(String id) {
        ProductDao dao = new ProductDaoImpl();
        Product product = dao.findProductDetailById(id);
        OrderDetails orderDetails = findProductToShoppingCart(id);
        if (orderDetails == null) {
            String sql = "insert into orderdetails(id, name, price, number) values(?, ?, ?, ?)";
            Integer number = 1;
            template.update(sql, product.getId(), product.getName(), product.getPrice(), number);
        } else {
            String sql = "update orderdetails set number = ? where order_id = ?";
            Integer order_id = orderDetails.getOrder_id();
            Integer number = orderDetails.getNumber();
            number += 1;
            template.update(sql, number, order_id);
        }
    }

    @Override
    public void plusProduct(String id) {
        OrderDetails orderDetails = findProductToShoppingCart(id);
        String sql = "update orderdetails set number = ? where order_id = ?";
        Integer order_id = orderDetails.getOrder_id();
        Integer number = orderDetails.getNumber();
        number += 1;
        template.update(sql, number, order_id);
    }

    @Override
    public void lessProduct(String id) {
        OrderDetails orderDetails = findProductToShoppingCart(id);
        String sql = "update orderdetails set number = ? where order_id = ?";
        Integer order_id = orderDetails.getOrder_id();
        Integer number = orderDetails.getNumber();
        number -= 1;
        template.update(sql, number, order_id);
    }

    /**
     * 根据商品id判断是否存在购物车中
     * @param id
     * @return
     */
    private OrderDetails findProductToShoppingCart(String id) {
        String sql = "select * from orderdetails where id = ?";
        OrderDetails orderDetails = null;
        try {
            orderDetails = template.queryForObject(sql, new BeanPropertyRowMapper<>(OrderDetails.class), id);
        } catch (Exception e) {
            return null;
        }
        return orderDetails;
    }
}
